//
//  UIButton+Extension.swift
//  SinaWeiBo
//
//  Created by ZKJ on 2017/8/24.
//  Copyright © 2017年 ZKJ. All rights reserved.
//

import UIKit

extension UIButton {
    // swift中类方法是以class开头的方法.类似于OC中+开头的方法
    /**
     class func creatButton(bgImgName:String, imageName:String) -> UIButton {
     let btn = UIButton()
     btn.setBackgroundImage(UIImage(named: bgImgName), for: .normal)
     btn.setBackgroundImage(UIImage(named: bgImgName + "_highlighted"), for: .highlighted)
     btn.setImage(UIImage(named: imageName), for: .normal)
     btn.setImage(UIImage(named: imageName + "_highlighted"), for: .highlighted)
     btn.sizeToFit()
     return btn
     }
     */
    
    // convenience : 便利,使用convenience修饰的构造函数叫做便利构造函数
    // 遍历构造函数通常用在对系统的类进行构造函数的扩充时使用
    // swift中推荐使用便利构造函数
    /*
     便利构造函数的特点
     1.便利构造函数通常都是写在extension里面
     2.便利构造函数init前面需要加convenience
     3.在便利构造函数中需要明确的调用self.init()
     */
    convenience init (bgImgName : String, imageName : String) {
        self.init()
        setBackgroundImage(UIImage(named: bgImgName), for: .normal)
        setBackgroundImage(UIImage(named: bgImgName + "_highlighted"), for: .highlighted)
        setImage(UIImage(named: imageName), for: .normal)
        setImage(UIImage(named: imageName + "_highlighted"), for: .highlighted)
        sizeToFit()
    }
    
    convenience init (title : String, titleColor : UIColor, font : UIFont, bgColor : UIColor) {
        self.init()
        setTitle(title, for: UIControlState.normal)
        setTitleColor(titleColor, for: UIControlState.normal)
        titleLabel?.font = font
        backgroundColor = bgColor
    }
}
